package com.wen.es.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wen.es.annotation.EsTypeAnno;
import com.wen.es.domain.ParentType;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 生成随机数据
 * Created by LAIO on 2018/7/1.
 */
public class DataGenerater {

    private static final char[] ALL_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890$ ".toCharArray();
    private static final int ALL_CHAR_LEN = ALL_CHARS.length;
    private static final Random random = new Random();

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        ParentType t = generateClass(ParentType.class);
        System.out.println(JSON.toJSONString(t, SerializerFeature.PrettyFormat));
    }

    public static <T> List<T> generateClassList(Class<T> clz) throws IllegalAccessException, InstantiationException {
        int size = random.nextInt(20);
        List<T> list = new LinkedList<>();
        for (int i = 0; i < size; i++) {
            list.add(generateClass(clz));
        }
        return list;
    }

    public static <T> T generateClass(Class<T> clz) throws IllegalAccessException, InstantiationException {
        T t = clz.newInstance();
        Field[] fields = clz.getDeclaredFields();
        EsTypeAnno type;
        for (Field fi : fields) {
            type = fi.getAnnotation(EsTypeAnno.class);
            fi.setAccessible(true);
            if (Objects.equals(type.type(), EsConstants.TYPE_DATE)) {
                fi.set(t, generateDate());
            } else if (Objects.equals(type.type(), EsConstants.TYPE_LONG)) {
                fi.set(t, generateLong());
            } else if (Objects.equals(type.type(), EsConstants.TYPE_DOUBLE)) {
                fi.set(t, generateDouble());
            } else if (Objects.equals(type.type(), EsConstants.TYPE_STRING)) {
                fi.set(t, generateString());
            } else if (Objects.equals(type.type(), EsConstants.TYPE_NESTED)) {
                int size = random.nextInt(25);
                List<Object> list = new LinkedList<>();
                for (int i = 0; i < size; i++) {
                    list.add(generateClass(type.refer()));
                }
                fi.set(t, list);
            }
        }
        return t;
    }

    public static long generateLong() {
        return 1000000L + random.nextLong() % 10000000000L;
    }

    public static double generateDouble() {
        return random.nextDouble() * 100000;
    }

    public static Date generateDate() {
        long curTime = System.currentTimeMillis();
        long diffTime = (random.nextInt() % 3 - 1) * (random.nextLong() % 86400000000L);
        return new Date(curTime + diffTime);
    }

    public static String generateString() {
        int len = 1 + random.nextInt(50);
        StringBuilder stringBuilder = new StringBuilder(len);
        stringBuilder.append(ALL_CHARS[random.nextInt(ALL_CHAR_LEN - 1)]);
        for (int i = 1; i < len; i++) {
            stringBuilder.append(ALL_CHARS[random.nextInt(ALL_CHAR_LEN)]);
        }
        return stringBuilder.toString();
    }
}
